﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace StoryTellerSite.Models
{
    public class PagerModel
    {
        public PagerModel()
        {
            this.ItemsPerPage = 10;
            this.pagerPartialViewName = "pvPager";
            this.QtdLinks = 10;
        }

        public int QtdLinks { get; set; }
        public int Current { get; set; }
        private int countPerPage;
        private int MaxLinks { get; set; }
        public int ItemsPerPage { get { return countPerPage; } set { countPerPage = value; } }
        public int TotalItems { get; set; }
        public int TotalPages
        {
            get
            {
                if (TotalItems > 0 && ItemsPerPage > 0)
                {
                    double dblPageCount = (double)((decimal)TotalItems / Convert.ToDecimal(ItemsPerPage));
                    return (int)Math.Ceiling(dblPageCount);
                }
                else
                    return 0;
            }
        }
        private string pagerPartialViewName;
        public string PagerPartialViewName { get { return pagerPartialViewName; } }
        public string Controller { get; set; }

        public bool ShowLinkFirst
        {
            get
            {
                if (Current > 1)
                    return true;
                return false;
            }
        }

        public bool ShowLinkLast
        {
            get
            {
                if (Current < TotalPages)
                    return true;
                return false;
            }
        }

        public int StatAt
        {
            get
            {
                if (TotalPages < QtdLinks)
                {
                    QtdLinks = TotalPages;
                    EndAt = TotalPages;
                    return 1;
                }
                else
                {
                    int qtd = (int)Math.Ceiling((double)QtdLinks / 2);
                    int ant = Current - qtd;
                    int pos = Current + qtd;

                    while (ant <= 0)
                    {
                        ant++;
                        pos++;
                    }

                    while (pos > (TotalPages + 1))
                    {
                        ant = ant > 0 ? ant-- : ant;
                        pos--;
                    }

                    EndAt = pos;
                    return ant;
                }
            }
        }

        public int EndAt
        {
            get;
            set;
        }
    }
}